package com.yan_jiu_sheng.daily.AC;

import java.util.Arrays;

/**
 * https://leetcode.cn/problems/right-triangles/description/?envType=daily-question&envId=2024-08-02
 * 统计+组合数
 * 自我AC通过
 *
 * @author yulongtian
 * @create 2024-08-02 10:38
 */
public class Test20240802 {
    public static void main(String[] args) {
        int[][] grid = {{0, 1, 0}, {0, 1, 1}, {0, 1, 0}};
        System.out.println(new Test20240802().numberOfRightTriangles(grid));
    }

    public long numberOfRightTriangles(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[] row = new int[m];
        int[] col = new int[n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    row[i]++;
                    col[j]++;
                }
            }
        }
        long ans = 0L;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    ans += (long) (row[i] - 1) * (col[j] - 1);
                }
            }
        }

        return ans;
    }

}
